package com.diamond.web.dto;

import java.util.Arrays;
import java.util.Map;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.springframework.web.bind.annotation.RequestMethod;

import com.diamond.web.model.system.Permission;

/**
 * 自定义 url与Class 的映射
 * 
 * @author Administrator
 * 
 */
public class UrlMappingDto implements Comparable<UrlMappingDto> {
	private String className;
	private String url;
	private String paramsClass;
	private String methodName;
	private String requestMethods;
	private String headers;
	private String produces;
	private String params;
	private String consumes;
	private Permission permission;
	private String returnType;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		url = url.replace("//", "/");
		url = url.startsWith("/") ? url : "/" + url;
		this.url = url;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getParamsClass() {
		return paramsClass;
	}

	public void setParamsClass(String paramsClass) {
		this.paramsClass = paramsClass;
	}

	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	public String getRequestMethods() {
		return requestMethods;
	}

	public void setRequestMethods(String requestMethods) {
		this.requestMethods = requestMethods;
	}

	public String getHeaders() {
		return headers;
	}

	public void setHeaders(String headers) {
		this.headers = headers;
	}

	public String getProduces() {
		return produces;
	}

	public void setProduces(String produces) {
		this.produces = produces;
	}

	public String getParams() {
		return params;
	}

	public void setParams(String params) {
		this.params = params;
	}

	public String getConsumes() {
		return consumes;
	}

	public void setConsumes(String consumes) {
		this.consumes = consumes;
	}

	@JsonIgnore
	public void setParamsClass(Class<?>[] parameterTypes) {
		if (parameterTypes != null) {
			this.paramsClass = arr2String(parameterTypes);
		}
	}

	public String arr2String(Object[] arr) {
		return Arrays.toString(arr);
	}

	public void setRequestMethods(RequestMethod[] method) {
		if (method != null) {
			this.requestMethods = arr2String(method);
		}
	}

	public void setHeaders(String[] headers) {
		if (headers != null) {
			this.headers = arr2String(headers);
		}
	}

	public void setProduces(String[] produces2) {
		if (produces2 != null) {
			this.produces = arr2String(produces2);
		}
	}

	public void setParams(String[] params2) {
		if (params2 != null) {
			this.params = arr2String(params2);
		}
	}

	public void setConsumes(String[] consumes2) {
		if (consumes2 != null) {
			this.consumes = arr2String(consumes2);
		}
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("url:" + url + "\n");
		sb.append("className:" + className + "\n");
		sb.append("paramsClass:" + paramsClass + "\n");
		sb.append("methodName:" + methodName + "\n");
		sb.append("requestMethods:" + requestMethods + "\n");
		sb.append("headers:" + headers + "\n");
		sb.append("produces:" + produces + "\n");
		sb.append("params:" + params + "\n");
		sb.append("consumes:" + consumes + "\n");
		return sb.toString();
	}

	@Override
	public int compareTo(UrlMappingDto o) {
		if (o != null) {
			return this.className.compareTo(o.className);
		} else {
			return -1;
		}
	}

	public Permission getPermission() {
		return permission;
	}

	public void setPermission(Permission permission) {
		this.permission = permission;
	}

	public String getReturnType() {
		return returnType;
	}

	public void setReturnType(String returnType) {
		this.returnType = returnType;
	}

	public void setPermission(Map<String, Permission> urlPermissionMap) {
		if (urlPermissionMap != null) {
			this.permission = urlPermissionMap.get(url);
		}
	}
}
